<?php
	/**
	 * @package AmpleHarvest
	 */
	require_once 'TableHeader.php';

	/**
	 * @brief This class Describes the Query_History Table in the AmpleHarvest Database
	 *
	 * - Revision History
	 *  - 0.1
	 * 	 - <b>Date</b>: 07 NOV 2011
	 * 	 - <b>Author</b>: Randy Blancett
	 * 	  - Initial Version
	 *
	 * @author Randy Blancett
	 * @version 0.1
	 * @ingroup code classes table
	 */
	class cTABLE_UserGroups extends cTable
	{

		/**
		 * @{
		 * @name Columns
		 */
		public $m_int_COL_ID = null;
		public $m_int_COL_Access = null;
		public $m_int_COL_Name = null;
		public $m_int_COL_Requester = null;
		public $m_int_COL_Justification = null;
		public $m_int_COL_Desc = null;
		public $m_int_COL_Active = null;
		public $m_int_COL_Disapproved = null;
		public $m_int_COL_DisaprovalReason = null;
		public $m_int_COL_Classification = null;
		public $m_int_COL_SCI = null;
		public $m_int_COL_Distro = null;
		/**
		 * @}
		 */

		/**
		 * This constructor sets all the required column data
		 */
		public function __construct()
		{
			$this->m_str_TableName = "user_groups";
			$this->set_Engine(self::C_STR_ENGINE_INNODB);

			//Create Column Objects
			$obj_COL_ID = new cColVarChar("id");
			$obj_COL_ID->set_Length(36);
			$obj_COL_ID->set_Index(abs_Column::c_int_INDEX_PRIMARY);
			$obj_COL_ID->set_Comment("This column holds the ID of the referance.");
			$obj_COL_ID->set_Editable(false);
			$obj_COL_ID->set_Label("ID");

			$obj_COL_Access = new cColTinyInt("access");
			$obj_COL_Access->set_Index(abs_Column::c_int_INDEX_INDEX);
			$obj_COL_Access->set_Comment("This column holds the Access level from 0-2.");
			$obj_COL_Access->set_Label("Access");			
			
			$obj_COL_Name = new cColVarChar("name");
			$obj_COL_Name->set_Length(200);
			$obj_COL_Name->set_Comment("This column holds the name of the group.");
			$obj_COL_Name->set_Label("Group Name");

			$obj_COL_Requester = new cColVarChar("requester");
			$obj_COL_Requester->set_Length(36);
			$obj_COL_Requester->set_Index(abs_Column::c_int_INDEX_INDEX);
			$obj_COL_Requester->set_Comment("This column holds the ID of the User that requested the Group.");
			$obj_COL_Requester->set_Editable(false);
			$obj_COL_Requester->set_Label("Requester");

			$obj_COL_Justification = new cColBlob("justification");
			$obj_COL_Justification->set_Comment("This is the Justification for the group creation.");
			$obj_COL_Justification->set_Label("Justification");

			$obj_COL_Desc = new cColBlob("description");
			$obj_COL_Desc->set_Comment("This is the Description of the group.");
			$obj_COL_Desc->set_Label("Description");

			$obj_COL_Active = new cColTinyInt("active");
			$obj_COL_Active->set_Index(abs_Column::c_int_INDEX_INDEX);
			$obj_COL_Active->set_Comment("The 1 if group is active 0 if group is not.");
			$obj_COL_Active->set_Label("Active");

			$obj_COL_Disapproved = new cColTinyInt("disapproved");
			$obj_COL_Disapproved->set_Index(abs_Column::c_int_INDEX_INDEX);
			$obj_COL_Disapproved->set_Comment("The 1 if group is disapproved 0 if group is not.");
			$obj_COL_Disapproved->set_Label("Disapproved");

			$obj_COL_DisaprovalReason = new cColBlob("disapproval_reason");
			$obj_COL_DisaprovalReason->set_Comment("The reason the group was disapproved.");
			$obj_COL_DisaprovalReason->set_Label("Disaproval Reason");

			$obj_COL_Classification = new cColVarChar("classification");
			$obj_COL_Classification->set_Length(36);
			$obj_COL_Classification->set_Comment("This column holds the classification of the Request.");
			$obj_COL_Classification->set_Label("Classification");

			$obj_COL_SCI = new cColVarChar("sci");
			$obj_COL_SCI->set_Length(100);
			$obj_COL_SCI->set_Comment("This column holds the SCI portion of the classification of the Request.");
			$obj_COL_SCI->set_Label("SCI");

			$obj_COL_Distro = new cColVarChar("distro");
			$obj_COL_Distro->set_Length(100);
			$obj_COL_Distro->set_Comment("This column holds the Distro portion of the classification of the Request.");
			$obj_COL_Distro->set_Label("Distro");

			//Set up connection
			$this->m_obj_Connection = new cConnection("AmpleHarvest", "admin");

			//Activate all Debug options
			$this->m_obj_Connection->set_DebugLevel(cLog_Connection_Debug::c_int_LEVEL_HIDDEN);
			$this->m_obj_Connection->set_LogLevel(cLog_Connection::c_int_LEVEL_BASIC);
			//$this->m_obj_Connection->activate_Logging();
			//$this->m_obj_Connection->activate_LogDebug();
			//$this->m_obj_Connection->activate_ScreenDebug();

			$this->m_int_COL_ID = $this->add_Column($obj_COL_ID);
			$this->m_int_COL_Access = $this->add_Column($obj_COL_Access);
			$this->m_int_COL_Active = $this->add_Column($obj_COL_Active);
			$this->m_int_COL_Classification = $this->add_Column($obj_COL_Classification);
			$this->m_int_COL_Desc = $this->add_Column($obj_COL_Desc);
			$this->m_int_COL_Disapproved = $this->add_Column($obj_COL_Disapproved);
			$this->m_int_COL_DisaprovalReason = $this->add_Column($obj_COL_DisaprovalReason);
			$this->m_int_COL_Distro = $this->add_Column($obj_COL_Distro);
			$this->m_int_COL_Justification = $this->add_Column($obj_COL_Justification);
			$this->m_int_COL_Name = $this->add_Column($obj_COL_Name);
			$this->m_int_COL_Requester = $this->add_Column($obj_COL_Requester);
			$this->m_int_COL_SCI = $this->add_Column($obj_COL_SCI);
		}

		/**
		 * This function is used to insert Data into the database
		 */
		public function insert_Data2DB()
		{
			if (!$this->get_id())
			{
				$this->set_id($this->create_GUID());
			}

			return parent::insert_Data2DB();
		}

		/**
		 * Saves Information to the Database
		 */
		public function save_Data2DB()
		{
			if ($this->validate_ID($this->get_id()))
			{
				return $this->update_Data2DB();
			}
			else
			{
				return $this->insert_Data2DB();
			}
		}

		public function insert_DefaultData()
		{
			return true;
		}

		/**
		 * Returns if the id is a valid and existing ID
		 * @param string $str_User_ID ID of the Page to check (GUID)
		 * @return boolean true if valid false if not
		 */
		public function validate_ID($str_ID)
		{
			// ensure that there is an ID passed and that it is a valid GUID
			if (!empty($str_ID) && preg_match(self::c_REG_GUID_VALID, $str_ID))
			{
				$this->clear_Query();
				$this->add_ColumnQuery($this->m_int_COL_ID, $str_ID);
				if ($this->do_Search() > 0)
				{
					return true;
				}
			}
			return false;
		}

		/**
		 * Loads information into object from Database based on ID passed
		 * @param string $str_ID ID of the id to check (int)
		 * return boolean true if loaded false if not
		 */
		public function load_DataFromID($str_ID)
		{
			$this->clear();

			if ($this->validate_ID($str_ID))
			{
				$this->clear_Query();
				$this->add_ColumnQuery($this->m_int_COL_ID, $str_ID);
				if ($this->do_Search() > 0)
				{
					$obj_RS = $this->get_ResultSet();
					if ($obj_RS[0])
					{
						$this->load_DataFromArray($obj_RS[0]);
						return true;
					}
				}
			}
			return false;
		}
	}
?>
